import os,sys,yaml
#获取项目所在文件夹 即ea999/
pro=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
print(pro)
class test:

    def run(self):
        temp_data=set()#依赖优先加载集合
        data={}
        js_str=""
        css_str=""
        #要保存到的js文件
        self.save_js_path=os.path.join(pro,"easy_edit","_static_dir","default.js")
        #要保存到的css文件
        self.save_css_path=os.path.join(pro,"easy_edit","_static_dir","default.css")
        #获取到前端 文件夹
        self.html_dir=os.path.join(pro,"easy_edit","_web","vue_template_store")
        #获取默认/编辑器 组件
        self.default_dir=self.html_dir+os.sep+'a1'
        #编辑目录1
        dir1_lst=os.listdir(self.default_dir)
        data["a1"]={}
        for dir1_id in dir1_lst:
            #目录1完整路径
            dir1_path=self.default_dir+os.sep+dir1_id
            if not os.path.isdir(dir1_path):continue
            #目录2
            dir2_lst=[item  for item in os.listdir(dir1_path)if '.'not in item]
            #读取目录信息 如名称
            if os.path.exists(dir1_path+os.sep+'main.yaml'):
                with open(dir1_path+os.sep+'main.yaml', 'r',encoding='utf-8') as f:
                    yaml_val1 = yaml.load(f, Loader=yaml.FullLoader)
            else:
                continue
            data["a1"][dir1_id]=yaml_val1
            data["a1"][dir1_id]['child']={}
            for dir2_id in dir2_lst:
                #目录2完整路径
                # print("dir1_path",dir1_path,dir2_id)
                dir2_path=dir1_path+os.sep+dir2_id
                if not os.path.isdir(dir2_path):continue
                #组件目录
                temp_lst=[item for item in os.listdir(dir2_path)if'.'not in item]
                #读取目录信息 如名称
                if os.path.exists(dir2_path+os.sep+'main.yaml'):
                    with open(dir2_path+os.sep+'main.yaml', 'r',encoding='utf-8') as f:
                        yaml_val2 = yaml.load(f, Loader=yaml.FullLoader)
                else:
                    continue
                data["a1"][dir1_id]['child'][dir2_id]=yaml_val2
                data["a1"][dir1_id]['child'][dir2_id]['child']={}
                for temp_id in temp_lst:
                    #组件完整目录
                    temp_path=dir2_path+os.sep+temp_id
                    if not os.path.isdir(temp_path):continue
                    #读取组件配置文件 获取到当前组件的最新版本号
                    temp_config_path=temp_path+os.sep+"main.yaml"
                    if os.path.exists(temp_config_path):
                        with open(temp_config_path, 'r',encoding='utf-8') as f:
                            yaml_val3 = yaml.load(f, Loader=yaml.FullLoader)
                        version=str(yaml_val3.get("index"))
                    else:
                        continue
                    #获取当前版本目录数据
                    version_path=temp_path+os.sep+"history"+os.sep+'a'+version
                    #js文件
                    js_path=version_path+os.sep+"main.js"
                    css_path=version_path+os.sep+"main.css"
                    yaml_path3=version_path+os.sep+"main.yaml"
                    if os.path.exists(yaml_path3):
                        with open(yaml_path3, 'r',encoding='utf-8') as f:
                            yaml_val4= yaml.load(f, Loader=yaml.FullLoader)
                        yaml_val3.update(yaml_val4)
                    else:
                        continue
                    use=yaml_val4.get("use")
                    if use is not None and len(use):
                        for path in use:
                            if path in temp_data:continue
                            else:
                                temp_data.add(path)
                                t_lst=path.split(".")
                                dir_p=os.path.join(self.html_dir,'a'+t_lst[0],'a'+t_lst[1],'a'+t_lst[2],'a'+t_lst[3],'history','a'+t_lst[4])
                                js_path2=dir_p+os.sep+"main.js"
                                css_path2=dir_p+os.sep+"main.css"
                                with open(js_path2,'r',encoding='utf-8')as f:
                                    js_str+=(f.read()+'\n')
                                with open(css_path2,'r',encoding='utf-8')as f:
                                    css_str+=(f.read()+'\n')
                    data["a1"][dir1_id]['child'][dir2_id]['child'][temp_id]=yaml_val3
                    pa=("0."+dir1_id+'.'+dir2_id+'.'+temp_id+'.'+version).replace("a","")
                    if pa in temp_data:
                        continue
                    else:
                        temp_data.add(pa)
                    try:
                        with open(js_path,'r',encoding='utf-8')as f:
                            js_str+=(f.read()+'\n')
                        with open(css_path,'r',encoding='utf-8')as f:
                            css_str+=(f.read()+'\n')
                    except:pass
        with open(self.save_js_path,'w',encoding='utf-8')as f:
            f.write(js_str)
        with open(self.save_css_path,'w',encoding='utf-8')as f:
            f.write(css_str)
        return {"data":data}
if __name__=="__main__":
    test().run()